package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.mybatis.model.Admin;
import com.bsh.mybatis.model.Role;
import com.bsh.repository.DaoAdmin;
import com.bsh.repository.DaoAdminRole;
import com.bsh.repository.DaoRole;
import com.bsh.service.AdminService;
import com.bsh.utils.DesUtils;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 后台理员
 * @author gsh
 *
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
	@Autowired
	private DaoAdmin adminDao;
	@Autowired
	private DaoRole roleDao;
	@Autowired
	private DaoAdminRole adminRoleDao;
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#getLoginAdmin(java.lang.String, java.lang.String)
	 */
	@Override
	public Admin getLoginAdmin(String loginName,String password){	
		DesUtils des=new DesUtils();
		String enPassword=des.encryptPassword(password);
		Admin admin = this.adminDao.getLoginUser(loginName,enPassword);
		return admin;
	}
	
	@Override
	public Admin getLoginAdminByEncodePsw(String loginName,String encodePassword){	
		Admin admin = this.adminDao.getLoginUser(loginName,encodePassword);
		return admin;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#getPage(com.bsh.utils.Pager, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager,String userName,String email) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isNullOrEmpty(userName))
			sb.append(String.format(" and u.name = '%s'",userName.trim()));
		if(!StringUtils.isNullOrEmpty(email))
			sb.append(String.format(" and u.email = '%s'",email.trim()));		
		List<Map<String, Object>> list = adminDao.getPage(pager,sb.toString());
		return list;
	}
	
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#getAdmin(java.lang.Integer)
	 */
	@Override
	public Admin getAdmin(Integer adminid) {
		return adminDao.selectByPrimaryKey(adminid);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#addAdmin(com.bsh.mybatis.model.Admin)
	 */
	@Override
	public Integer addAdmin(Admin admin) {	
		DesUtils des=new DesUtils();
		admin.setPassword(des.encryptPassword(admin.getPassword()));
		return adminDao.addAdmin(admin);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#editAdmin(com.bsh.mybatis.model.Admin)
	 */
	@Override
	public Integer editAdmin(Admin admin) {
		return adminDao.editAdmin(admin);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#enableAdmin(int, boolean)
	 */
	@Override
	public Integer enableAdmin(int id, boolean isEnabele) {
		return adminDao.enableAdmin(id, isEnabele ? 1 : 0);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#disableBatch(int[])
	 */
	@Override
	public Integer disableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			adminDao.enableAdmin(id, 0);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#enableBatch(int[])
	 */
	@Override
	public Integer enableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			adminDao.enableAdmin(id, 1);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#deleteAdmin(int)
	 */
	@Override
	public Integer deleteAdmin(int adminId) {
		int res =  adminDao.deleteByPrimaryKey(adminId);
		adminRoleDao.removeByAdmin(adminId);
		return res;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#getAdminRoles(int)
	 */
	@Override
	public List<Role> getAdminRoles(int adminId){		
		return roleDao.getAdminRoles(adminId);
	}	

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.AdminService#resetPassword(int)
	 */
	@Override
	public Integer resetPassword(int adminId) {
		DesUtils des=new DesUtils();
		String enPassword=des.encryptPassword("888888");
		return adminDao.changePassword(adminId,enPassword);
	}
	
	/**
	 * 修改密码
	 * @param adminId
	 * @param password
	 * @param newPassword
	 * @return
	 */
	@Override
	public Integer changePassword(int adminId,String password,String newPassword) {
		DesUtils des=new DesUtils();		
		Admin admin = adminDao.selectByPrimaryKey(adminId);
		
		Integer res=0;
		String enPassword=des.encryptPassword(password);
		if(adminDao.getLoginUser(admin.getLoginName(), enPassword)!=null){
			String enNewPassword=des.encryptPassword(newPassword);
			res= adminDao.changePassword(adminId,enNewPassword);
		}
		return res;
	}	
}
